<template>
  <a-modal
    title='告警链详情'
    :width='640'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <s-table
      ref='table'
      size='default'
      rowKey='key'
      :columns='columns'
      :data='loadData'
    >
      <span slot='serial' slot-scope='text, record, index'>
        {{ index + 1 }}
      </span>
      <span slot='description' slot-scope='text'>
        <ellipsis :length='4' tooltip>{{ text }}</ellipsis>
      </span>

      <span slot='action' slot-scope='text, record'>
        <template>
          <a @click='handleEdit(record)'>配置</a>
          <a-divider type='vertical' />
          <a @click='handleSub(record)'>订阅报警</a>
        </template>
      </span>
    </s-table>
  </a-modal>
</template>

<script>
import ColorPicker from '@/components/ColorPicker/index.vue'
import pick from 'lodash.pick'
import { Ellipsis, STable } from '@/components'

// 表单字段
const fields = ['sys-type', 'equipment_type', 'point_type']

let id = 0
export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  components: {
    STable, Ellipsis,
    ColorPicker
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      form: this.$form.createForm(this),
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '时间',
          dataIndex: 'no'
        },
        {
          title: '源IP',
          dataIndex: 'description',
        },
        {
          title: '目的IP',
          dataIndex: 'id1'
          // needTotal: true,
          // customRender: (text) => text + ' 次'
        },
        {
          title: '告警属性',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        }
      ],
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return new Promise((resolve, reject) => {
          resolve({
            endRow: 10,
            hasNextPage: false,
            hasPreviousPage: false,
            isFirstPage: false,
            isLastPage: false,
            list: [{}],
            navigateFirstPage: 0,
            navigateLastPage: 0,
            navigatePages: 0,
            navigatepageNums: null,
            nextPage: 0,
            pageNum: 1,
            pageSize: 10,
            pages: 3,
            prePage: 0,
            size: 10,
            startRow: 0,
            total: 1
          })
        })
      }
    }
  },
  created() {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))
    this.form.getFieldDecorator('options', { initialValue: [{ id }], preserve: true })
    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  },
  methods: {
    remove(value) {
      const { form } = this
      const { id: currentId } = value
      // can use data-binding to get
      const options = form.getFieldValue('options')
      // We need at least one passenger
      if (options.length === 1) {
        return
      }

      // can use data-binding to set
      form.setFieldsValue({
        options: options.filter(item => item.id !== currentId)
      })
    },

    add() {
      const { form } = this
      // can use data-binding to get
      const options = form.getFieldValue('options')
      id++
      // important! notify form to detect changes
      form.setFieldsValue({
        options: [...options, { id }]
      })
    }
  }
}
</script>
